<!--
 * @Descripttion: 
 * @version: 
 * @Author: 尹鹏孝
 * @Date: 2021-12-22 08:57:05
 * @LastEditors: 尹鹏孝
 * @LastEditTime: 2021-12-22 09:32:51
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>左脚值</title>
</head>

<body>
    <script>
        //给定一个二叉树，在树的最后一行找到最左边的值。
        // 思路，找最下一层的最左边的值
        //loop
        var findBottomLeftValue = function (root) {
            let maxPath = 0, resNode = null;
            const defs = (node, curPath) => {
                if (node.left === null && node.right === null) {
                    if (curPath > maxPath) {
                        maxPath = curPath;
                        resNode = node.val;
                    }

                }
                node.left && defs(node.left, curPath + 1);
                node.right && defs(node.right, curPath + 1);
            }
            defs(root, 1);
            return resNode;
        }
        var findBottomLeftValue = function (root) {
            let queue = [];
            if (root === null) {
                return null
            }
            queue.push(root);
            let resNode = null;
            while (queue.length) {
                let length = queue.length;

                for (let i = 0; i < length; i++) {
                    let node = queue.shift();
                    if (i == 0) {
                        // 最左脚节点
                        resNode = node.val;
                    }
                    node.left && queue.push(node.left);
                    node.right && queue.push(node.right);
                }

            }
            return resNode;
        }
    </script>

</body>

</html>